home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / FILER / TARSRC.SPK / c / args next >
Text File  |  1994-05-02  |  3KB  |  133 lines

  1.  
  2. #include <ctype.h>
  3.  
  4. #include "tar.h"
  5. #include "options.h"
  6. #include "dir.h"
  7. #include "args.h"
  8.  
  9.  
  10. /* argument routines */
  11.  
  12.  
  13. static char **MyArgV,**InitialArgV;
  14.  
  15.  
  16. void ResetArgs(void) {
  17.   MyArgV = InitialArgV;
  18. } /* ResetArgs */
  19.  
  20.  
  21. void InitArgs(char *argv[]) {
  22.   FILE *ArgFile;
  23.   int ArgCnt,ArgSpace,Length;
  24.   char *Next,*cp;
  25.   char Buffer[256];
  26.  
  27.   if (UseListFile) {
  28.     ArgFile = fopen(listfile,"r");
  29.     if (ArgFile == NULL) {
  30.       fprintf(stderr,"tar: unable to open list file\n");
  31.       Terminate(24);
  32.     }
  33.     ArgCnt = 0;
  34.     ArgSpace = 0;
  35.     while (fgets(Buffer,256,ArgFile) != NULL) {
  36.       Length = strlen(Buffer);
  37.       if (Length <= 1)
  38.         continue;
  39.       Buffer[Length-1] = '\0';
  40.       if (Buffer[0] == '.') {
  41.         if (Length <= 3)
  42.           Next = NULL;
  43.         else {
  44.           Next = malloc(Length-2);
  45.           strcpy(Next,Buffer+2);
  46.         }
  47.         cp = Buffer+1;
  48.         if (CheckOption(&cp,Next) == 0 && Next != NULL)
  49.           free(Next);
  50.         continue;
  51.       }
  52.       if (ArgCnt+1 >= ArgSpace) {
  53.         ArgSpace += 50;
  54.         InitialArgV = realloc(InitialArgV,ArgSpace*sizeof(char *));
  55.         if (InitialArgV == NULL) {
  56.           fprintf(stderr,"tar: InitialArgV allocation fault\n");
  57.           Terminate(2);
  58.         }
  59.       }
  60.       InitialArgV[ArgCnt] = malloc(Length);
  61.       if (InitialArgV[ArgCnt] == NULL) {
  62.         fprintf(stderr,"tar: argument allocation fault\n");
  63.         Terminate(3);
  64.       }
  65.       strcpy(InitialArgV[ArgCnt],Buffer);
  66.       ArgCnt++;
  67.     }
  68.     InitialArgV = realloc(InitialArgV,ArgCnt*sizeof(char *));
  69.     if (InitialArgV == NULL) {
  70.       fprintf(stderr,"tar: InitialArgV reallocation fault\n");
  71.       Terminate(4);
  72.     }
  73.     InitialArgV[ArgCnt] = NULL;
  74.     fclose(ArgFile);
  75.   } else
  76.     InitialArgV = argv;
  77.   ResetArgs();
  78. } /* InitArgs */
  79.  
  80.  
  81. char *GetArg(int Inc) {
  82.   char *Arg;
  83.   
  84.   Arg = *MyArgV;
  85.   MyArgV += Inc;
  86.   return Arg;
  87. } /* GetArg */
  88.  
  89.  
  90. static int PrefixMatches(char *s1, char *s2) {
  91.   if (isArchie) {
  92.     while (*s1)
  93.       if (tolower(*s1++) != tolower(*s2++))
  94.         return 0;
  95.   } else {
  96.     while (*s1)
  97.       if (*s1++ != *s2++)
  98.         return 0;
  99.   }
  100.   if (*s2) {
  101.     return (*s2 == (isArchie ? FS_DIRSEP : UNIX_DIRSEP));
  102.   }
  103.   return 1;
  104. } /* PrefixMatches */
  105.  
  106.  
  107. int MatchesNextArg(char *startdir) {
  108.   char *cp;
  109.   int None;
  110.  
  111.   if (ExtractFromArchive) {
  112. #if WITH_PWD
  113.     ChangeCurrentDir(startdir);
  114. #endif
  115.   }
  116.   ResetArgs();
  117.   None = 1;
  118.   while (cp = GetArg(1), cp) {
  119.     if (ExtractFromArchive && !strcmp(cp, "-C") && (cp = GetArg(1), cp)) {
  120. #if WITH_PWD
  121.       ChangeCurrentDir(startdir);
  122.       ChangeCurrentDir(cp);
  123. #endif
  124.       continue;
  125.     }
  126.     None = 0;
  127.     if (PrefixMatches(cp, Block.Header.name)) {
  128.       return 1;
  129.     }
  130.   }
  131.   return None;
  132. } /* MatchesNextArg */
  133.